package cn.org.donald.service.impl;

import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.dto.IdsDto;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.framework.util.BeanUtil;
import cn.org.donald.mapper.GameFeatureMapper;
import cn.org.donald.pojo.dto.game.FeatureDto;
import cn.org.donald.pojo.entity.game.Feature;
import cn.org.donald.mapper.FeatureMapper;
import cn.org.donald.pojo.entity.game.GameFeature;
import cn.org.donald.service.game.FeatureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@Service(interfaceClass = FeatureService.class, version = "1.0.0", group = "donald")
public class FeatureServiceImpl extends ServiceImpl<FeatureMapper, Feature> implements FeatureService {

    @Autowired
    private FeatureMapper featureMapper;

    @Autowired
    private GameFeatureMapper gameFeatureMapper;

    /**
     * 新增特性
     *
     * @param featureDto
     * @return
     */
    @Override
    public boolean add(FeatureDto featureDto) {
        if (isExistByFeatureName(featureDto.getFeatureName())){
            throw new ServiceException("特性已存在,请使用其他特性名称");
        }
        Feature feature = new Feature();
        BeanUtil.copyProperties(featureDto,feature);
        return super.save(feature);
    }

    /**
     * 分页查询游戏特性列表
     *
     * @param pageDto
     * @return
     */
    @Override
    public IPage<Feature> findPage(PageDto<Feature> pageDto) {
        QueryWrapper<Feature> queryWrapper = null;
        Feature query = pageDto.getQuery();
        if (!BeanUtil.isEmptyOrClassFieldAllEmpty(query)){
            queryWrapper = new QueryWrapper<>(query);
        }
        return featureMapper.selectPage(pageDto, queryWrapper);
    }

    /**
     * 根据id修改特性
     *
     * @param featureDto
     * @return
     */
    @Override
    public boolean editById(FeatureDto featureDto) {
        QueryWrapper<Feature> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",featureDto.getId());
        queryWrapper.eq("feature_name",featureDto.getFeatureName());
        Integer count = featureMapper.selectCount(queryWrapper);
        if (count>0){
            throw new ServiceException("特性已存在,请使用其他特性名称");
        }
        Feature feature = new Feature();
        BeanUtil.copyProperties(featureDto,feature);
        return super.updateById(feature);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Feature findOneById(Long id) {
        return super.getById(id);
    }

    /**
     * 根据id删除特性
     *
     * @param idsDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(IdsDto idsDto) {
        // 删除特性记录
        int i = featureMapper.deleteBatchIds(idsDto.getIds());
        // 删除特性和游戏关联表记录
        if (i>0){
            QueryWrapper<GameFeature> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("feature_id",idsDto.getIds());
            int delete = gameFeatureMapper.delete(queryWrapper);
            return delete>0;
        }
        return false;
    }

    /**
     * 判断特性名称是否存在
     *
     * @param featureName
     * @return
     */
    @Override
    public boolean isExistByFeatureName(String featureName) {
        QueryWrapper<Feature> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("feature_name",featureName);
        return featureMapper.selectCount(queryWrapper)>0;
    }

    private List<Feature> findByEntity(Feature feature){
        QueryWrapper<Feature> queryWrapper = new QueryWrapper<>(feature);
        return featureMapper.selectList(queryWrapper);
    }
}
